{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import rabbitpy\n",
    "import time\n",
    "from ch6 import detect\n",
    "from ch6 import utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Open the connection and the channel\n",
    "connection = rabbitpy.Connection()\n",
    "channel = connection.channel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Create the worker queue\n",
    "queue_name = 'rpc-worker-%s' % os.getpid()\n",
    "queue = rabbitpy.Queue(channel, queue_name,\n",
    "                       auto_delete=True,\n",
    "                       durable=False,\n",
    "                       exclusive=True)\n",
    "\n",
    "# Declare the worker queue\n",
    "if queue.declare():\n",
    "    print('Worker queue declared')\n",
    "\n",
    "# Bind the worker queue\n",
    "if queue.bind('headers-rpc-requests',\n",
    "              arguments={'x-match': 'all',\n",
    "                         'source': 'profile',\n",
    "                         'object': 'image',\n",
    "                         'action': 'new'}):\n",
    "    print('Worker queue bound')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Consume messages from RabbitMQ\n",
    "for message in queue.consume_messages():\n",
    "\n",
    "    # Display how long it took for the message to get here\n",
    "    duration = time.time() - int(message.properties['timestamp'].strftime('%s'))\n",
    "    print('Received RPC request published %.2f seconds ago' % duration)\n",
    "\n",
    "    # Write out the message body to a temp file for facial detection process\n",
    "    temp_file = utils.write_temp_file(message.body,\n",
    "                                      message.properties['content_type'])\n",
    "\n",
    "    # Detect faces\n",
    "    result_file = detect.faces(temp_file)\n",
    "\n",
    "    # Build response properties including the timestamp from the first publish\n",
    "    properties = {'app_id': 'Chapter 6 Listing 2 Consumer',\n",
    "                  'content_type': message.properties['content_type'],\n",
    "                  'correlation_id': message.properties['correlation_id'],\n",
    "                  'headers': {\n",
    "                      'first_publish': message.properties['timestamp']}}\n",
    "\n",
    "    # The result file could just be the original image if nothing detected\n",
    "    body = utils.read_image(result_file)\n",
    "\n",
    "    # Remove the temp file\n",
    "    os.unlink(temp_file)\n",
    "\n",
    "    # Remove the result file\n",
    "    os.unlink(result_file)\n",
    "\n",
    "    # Publish the response response\n",
    "    response = rabbitpy.Message(channel, body, properties, opinionated=True)\n",
    "    response.publish('rpc-replies', message.properties['reply_to'])\n",
    "\n",
    "    # Acknowledge the delivery of the RPC request message\n",
    "    message.ack()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
